<?php if (empty($this->data)) : // ถ้าไม่มีข้อมูล ?>
    <?php echo $this->element('dataNotFound'); ?>
<? else : // ถ้ามีข้อมูล ?>
<table class="data">
    <thead>
        <tr>
            <th class="no" rowspan="2">No.</th>
            <th class="text-left" colspan="3">Cost Center</th>
            <th width="50px" rowspan="2">Freq.</th>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <th width="90px" rowspan="2">Price</th>
                <?php endif; ?>
            <th colspan="3">Quantity</th>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <th width="90px" rowspan="2">Amount</th>
                <?php endif; ?>
        </tr>
        <tr>
            <th width="185px">Stock code <?php if ($display_stock_out_no == true) echo '/ <br /> Stock out no.'; ?></th>
            <th width="140px">Vendor Part no. <?php if ($display_stock_out_no == true) echo '/ <br /> Stock out date'; ?></th>
            <th>Vendor <?php if ($display_stock_out_no == true) echo '/ Product Description / Remark'; ?></th>
            <th width="50px">Stock out</th>
            <th width="50px">Return</th>
            <th width="50px">Net</th>
        </tr>
    </thead>
    <tbody>
            <?php
            $total_freq = 0;
            $total_net_qty = 0;
            $total_qty = 0;
            $total_return = 0;
            $total_amount = 0;
            foreach($this->data as $i => $data): ?>
        <tr class="background1">
            <td class="center"><?php echo $i + 1; ?></td>
            <td class="bold" colspan="3"><?php echo $data['SiteCostCenter']['name']; ?></td>
            <td class="bold text-right"><?php echo $data['SiteCostCenter']['frequency']; ?></td>
                    <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <td class="bold text-right">-</td>
                    <?php endif; ?>
            <td class="bold text-right"><?php echo $data['SiteCostCenter']['stock_out_qty']; ?></td>
            <td class="bold text-right"><?php echo $data['SiteCostCenter']['return_qty']; ?></td>
            <td class="bold text-right"><?php echo $data['SiteCostCenter']['net_stock_out_qty']; ?></td>
                    <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <td class="bold text-right">
                            <?php echo number_format($data['SiteCostCenter']['amount'], 2); ?>
            </td>
                    <?php endif; ?>
                    <?php
                    $total_freq += $data['SiteCostCenter']['frequency'];
                    $total_qty += $data['SiteCostCenter']['stock_out_qty'];
                    $total_return += $data['SiteCostCenter']['return_qty'];
                    $total_net_qty += $data['SiteCostCenter']['net_stock_out_qty'];
                    $total_amount += $data['SiteCostCenter']['amount'];
                    ?>
        </tr>
        <?php foreach($data['CustProductSite'] as $cps) : ?>
            <tr class="<?php echo ($display_stock_out_no? "bold": ""); ?> backgroud2">
                <td class="center">&nbsp;</td>
                <td>
                            <?php echo $html->link($cps['CustProductSite']['stock_code'],
                            '/cust_product_sites/view/' . $cps['CustProductSite']['id'] . '/' . strtotime('now'),
                            array('target' => '_blank')); ?>
                </td>
                <td><?php echo $cps['CustStockOutHead'][0]['dispatch_from'] == ITS_PREFIX? $cps['Product']['part_no']: $cps['Product']['supplier_part_no']; ?></td>
                <td>
                            <?php echo $cps['CustStockOutHead'][0]['dispatch_from'] == ITS_PREFIX?
                            $this->requestAction('system_preferences/get/company_name'): $cps['Supplier']['name']; ?><br />
                    <?php echo $cps[0]['text']; ?>
                </td>
                <td class="text-right"><?php echo $cps[0]['frequency']; ?></td>
                        <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right"><?php echo number_format($cps[0]['price'], 2); ?></td>
                        <?php endif; ?>
                <td class="text-right"><?php echo $cps[0]['stock_out_qty']; ?></td>
                <td class="text-right"><?php echo $cps[0]['return_qty']; ?></td>
                <td class="text-right"><?php echo $cps[0]['net_stock_out_qty']; ?></td>
                        <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right"><?php echo number_format($cps[0]['price'] * $cps[0]['net_stock_out_qty'], 2); ?></td>
                        <?php endif; ?>
            </tr>
                    <?php if ($display_stock_out_no == true) : ?>
                        <?php
                        // ตัวแปรสำหรับลำดับเลข stock out no
                        $k = 1;
                        foreach($cps['CustStockOutHead'] as $sto) :
                            ?>
            <tr class="background3">
                <td>&nbsp;</td>
                                <?php if ($display_stock_out_no == true) : ?>
                <td>
                    <span><?php echo $k; ?>.</span>&nbsp;
                    <span>
                                            <?php echo $html->link($sto['stock_out_no'],
                                            '/cust_stocks_out/view/' . $sto['id'] . '/' . strtotime('now'),
                                            array('target' => '_blank')); ?>
                    </span>
                </td>
                <td>&nbsp;<?php echo $sto['stock_out_date']; ?></td>
                <td>
                    <?php echo $sto['remark']; ?>
                </td>
                <td>&nbsp;</td>
                                    <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td>&nbsp;</td>
                                    <?php endif; ?>
                <td class="text-right"><?php echo $sto['stock_out_qty']; ?></td>
                <td class="text-right"><?php echo $sto['return_qty']; ?></td>
                <td class="text-right"><?php echo $sto['net_stock_out_qty']; ?></td>
                                    <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right"><?php echo number_format($sto['net_stock_out_qty'] * $cps[0]['price'], 2); ?></td>
                                    <?php endif; ?>
                                <?php endif; ?>
            </tr>
                            <?php ++$k;
                        endforeach; ?>
                    <?php endif; ?>
                <?php endforeach; ?>
            <?php endforeach; ?>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="4" class="bold text-right">Grand Total</td>
            <td class="text-right bold"><?php echo number_format($total_freq); ?></td>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <td class="text-right">-</td>
                <?php endif; ?>
            <td class="text-right bold"><?php echo number_format($total_qty); ?></td>
            <td class="text-right bold"><?php echo number_format($total_return); ?></td>
            <td class="text-right bold"><?php echo number_format($total_net_qty); ?></td>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <td class="text-right bold"><?php echo number_format($total_amount, 2); ?></td>
                <?php endif; ?>
        </tr>
    </tfoot>
</table>
<?php //debug($this->data); ?>
<? endif; ?>